package cn.edu.scau.cmil.shishuai.comprehensive.address;

import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;

import javax.swing.JOptionPane;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import cn.edu.scau.cmil.shishuai.comprehensive.address.model.Person;
import cn.edu.scau.cmil.shishuai.comprehensive.address.model.PersonListWrapper;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


public class MainApp extends Application {

	private Stage primaryStage= new Stage();;
	private AnchorPane rootLayout;
    private Stage tempStage = new Stage();
    
    private ObservableList<Person> personData = FXCollections.observableArrayList();

    public ObservableList<Person> getPersonData() {
        return personData;
    }
    public void addPersonData(Person person) {
    	this.personData.add(person);
    }
    
    
	@Override
	public void start(Stage primaryStage){
		
		this.primaryStage = primaryStage;
        this.primaryStage.setTitle("成绩管理系统");
        this.primaryStage.setResizable(false);
		
		showLoginview();

		
		
	}

	public void initRootLayout() {
		try {
            // Load root layout from fxml file.
			primaryStage.setTitle("成绩管理系统");
			primaryStage.setResizable(false);
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/Main.fxml"));
            rootLayout = (AnchorPane) loader.load();
            
            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
		
	}
	
	public void showLoginview() {
        try {
            // Load login overview.
			Stage stage = new Stage();
			stage.setTitle("欢迎使用成绩管理系统");
			stage.setResizable(false);
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/Login.fxml"));
            AnchorPane Login = (AnchorPane) loader.load();

            Scene scene = new Scene(Login);
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public void showStudentSignview() {
        try {

            tempStage.setTitle("注册你的账号");
            tempStage.setResizable(false);
                    
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/StudentSign.fxml"));
            AnchorPane StudentSign = (AnchorPane) loader.load();
            
            Scene scene = new Scene(StudentSign);
            tempStage.setScene(scene);
            tempStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	public void Studentview() {
        try {

            tempStage.setTitle("你的成绩");
            tempStage.setResizable(false);
                    
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/secondMain.fxml"));
            AnchorPane StudentSign = (AnchorPane) loader.load();
            
            Scene scene = new Scene(StudentSign);
            tempStage.setScene(scene);
            tempStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	public Stage getPrimaryStage() {
        return primaryStage;
    }

	public static void main(String[] args) {
		launch(args);
	}
	
	public File getPersonFilePath() {
	    Preferences prefs = Preferences.userNodeForPackage(MainApp.class);
	    String filePath = prefs.get("filePath", null);
	    if (filePath != null) {
	        return new File(filePath);
	    } else {
	        return null;
	    }
	}


	public void setPersonFilePath(File file) {
	    Preferences prefs = Preferences.userNodeForPackage(MainApp.class);
	    if (file != null) {
	        prefs.put("filePath", file.getPath());

	        // Update the stage title.
	        //primaryStage.setTitle("AddressApp - " + file.getName());
	    } else {
	        prefs.remove("filePath");

	        // Update the stage title.
	        //primaryStage.setTitle("AddressApp");
	    }
	}
	public void loadPersonDataFromFile(File file) {
	    try {
	        JAXBContext context = JAXBContext
	                .newInstance(PersonListWrapper.class);
	        Unmarshaller um = context.createUnmarshaller();

	        // Reading XML from the file and unmarshalling.

	        PersonListWrapper wrapper = new PersonListWrapper();
	        wrapper = (PersonListWrapper) um.unmarshal(file);
	        //System.out.println(wrapper.getPersons().get(1).toString());
	        personData.clear();
	        personData.addAll(wrapper.getPersons());


	        // Save the file path to the registry.
	        setPersonFilePath(file);

	    } catch (Exception e) { // catches ANY exception

	    	JOptionPane.showMessageDialog(null, "加载失败", "错误", JOptionPane.ERROR_MESSAGE);

	    }
	}


	public void savePersonDataToFile(File file) {
	    try {
	        JAXBContext context = JAXBContext
	                .newInstance(PersonListWrapper.class);
	        Marshaller m = context.createMarshaller();
	        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	        

	        // Wrapping our person data.
	        PersonListWrapper wrapper = new PersonListWrapper();
	        wrapper.setPersons(personData);

	        // Marshalling and saving XML to the file.
	        m.marshal(wrapper, file);

	        // Save the file path to the registry.
	        setPersonFilePath(file);
	    } catch (Exception e) { // catches ANY exception

	    	JOptionPane.showMessageDialog(null, "错误", "请检查", JOptionPane.ERROR_MESSAGE);

	    }
	}
	
	public Person findperson(String user) {
		Person person = null;
		for(int i=0;i<personData.size();i++) {
			if(personData.get(i).getUser().equals(user)) {
				person = personData.get(i); 
			}
				

		}
		return person;
	}
	
}
